package com.student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentGradeManagementSystem {

    public static void main(String[] args) {
        Menu menu = new Menu();
        menu.displayMenu();
    }
}

class Student {
    private String name;
    private String id;
    private String course;
    private double grade;

    public Student(String name, String id, String course, double grade) {
        this.name = name;
        this.id = id;
        this.course = course;
        this.grade = grade;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    public String getCourse() {
        return course;
    }

    public double getGrade() {
        return grade;
    }

    @Override
    public String toString() {
        return "姓名: " + name + ", 学号: " + id + ", 课程: " + course + ", 成绩: " + grade;
    }
}

class GradeManager {
    private List<Student> students;

    public GradeManager() {
        students = new ArrayList<>();
    }

    public void addStudent(Student student) {
        students.add(student);
    }

    public List<Student> findStudentsByName(String name) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equals(name)) {
                result.add(student);
            }
        }
        return result;
    }

    public Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    public List<Student> findStudentsByCourse(String course) {
        List<Student> result = new ArrayList<>();
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                result.add(student);
            }
        }
        return result;
    }

    public double getAverageGradeByCourse(String course) {
        double sum = 0;
        int count = 0;
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                sum += student.getGrade();
                count++;
            }
        }
        return count == 0 ? 0 : sum / count;
    }

    public double getMaxGradeByCourse(String course) {
        double max = Double.MIN_VALUE;
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                if (student.getGrade() > max) {
                    max = student.getGrade();
                }
            }
        }
        return max;
    }

    public double getMinGradeByCourse(String course) {
        double min = Double.MAX_VALUE;
        for (Student student : students) {
            if (student.getCourse().equals(course)) {
                if (student.getGrade() < min) {
                    min = student.getGrade();
                }
            }
        }
        return min;
    }
}

class Menu {
    private GradeManager gradeManager;
    private Scanner scanner;

    public Menu() {
        gradeManager = new GradeManager();
        scanner = new Scanner(System.in);
    }

    public void displayMenu() {
        while (true) {
            System.out.println("=================================");
            System.out.println("欢迎使用学生成绩管理系统");
            System.out.println("=================================");
            System.out.println("请选择操作：");
            System.out.println("1. 记录学生成绩");
            System.out.println("2. 查询学生成绩");
            System.out.println("3. 统计课程成绩");
            System.out.println("4. 退出系统");
            System.out.print("请输入选项序号：");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除缓冲区

            switch (choice) {
                case 1:
                    addStudentGrade();
                    break;
                case 2:
                    queryStudentGrade();
                    break;
                case 3:
                    showCourseStatistics();
                    break;
                case 4:
                    exitSystem();
                    return;
                default:
                    System.out.println("无效的选择，请重新输入");
            }
        }
    }

    private void addStudentGrade() {
        System.out.println("===== 记录学生成绩 =====");
        System.out.print("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        System.out.print("请输入成绩（0-100）：");
        double grade = scanner.nextDouble();
        scanner.nextLine(); // 清除缓冲区

        if (grade < 0 || grade > 100) {
            System.out.println("成绩无效，请输入0-100之间的分数");
            return;
        }

        Student student = new Student(name, id, course, grade);
        gradeManager.addStudent(student);
        System.out.println("成绩已成功记录！");
    }

    private void queryStudentGrade() {
        System.out.println("===== 查询学生成绩 =====");
        System.out.println("请选择查询方式：");
        System.out.println("1. 按学生姓名查询");
        System.out.println("2. 按学生学号查询");
        System.out.println("3. 按课程名称查询");
        System.out.print("请输入选项序号：");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 清除缓冲区

        switch (choice) {
            case 1:
                System.out.print("请输入学生姓名：");
                String name = scanner.nextLine();
                List<Student> studentsByName = gradeManager.findStudentsByName(name);
                if (!studentsByName.isEmpty()) {
                    for (Student student : studentsByName) {
                        System.out.println(student);
                    }
                } else {
                    System.out.println("未找到该学生");
                }
                break;
            case 2:
                System.out.print("请输入学生学号：");
                String id = scanner.nextLine();
                Student studentById = gradeManager.findStudentById(id);
                if (studentById != null) {
                    System.out.println(studentById);
                } else {
                    System.out.println("未找到该学生");
                }
                break;
            case 3:
                System.out.print("请输入课程名称：");
                String course = scanner.nextLine();
                List<Student> studentsByCourse = gradeManager.findStudentsByCourse(course);
                if (!studentsByCourse.isEmpty()) {
                    for (Student student : studentsByCourse) {
                        System.out.println(student);
                    }
                } else {
                    System.out.println("未找到该课程的学生");
                }
                break;
            default:
                System.out.println("无效的选择");
        }
    }

    private void showCourseStatistics() {
        System.out.println("===== 统计课程成绩 =====");
        System.out.print("请输入课程名称：");
        String course = scanner.nextLine();
        double average = gradeManager.getAverageGradeByCourse(course);
        double max = gradeManager.getMaxGradeByCourse(course);
        double min = gradeManager.getMinGradeByCourse(course);

        System.out.println("课程: " + course);
        System.out.printf("平均分: %.2f\n", average);
        System.out.println("最高分: " + max);
        System.out.println("最低分: " + min);
    }

    private void exitSystem() {
        System.out.println("感谢使用学生成绩管理系统，再见！");
    }
}
